package com.book.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.book.dto.OrderDto;
import com.book.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderDao extends BaseMapper<Order> {

    @Select("select \n" +
            "   a.id,a.order_time as orderTime,a.amount,a.price,a.status,a.user_id as userId,a.book_id as bookId, \n" +
            "   (select name from address where id=a.address_id) as name, \n" +
            "   (select phone from address where id=a.address_id) as phone, \n" +
            "   (select detailed_address from address where id=a.address_id)  as detailedAddress, \n" +
            "   (select book_name from book where id=a.book_id) as bookName \n" +
            " from  \n" +
            "   order_form a\n" +
            " where \n" +
            "   ( \n" +
            "       (#{userIdAdmin}!='' and 4!=#{status} and a.status=#{status} and a.del_flag=0) or  \n" +
            "       (#{userIdAdmin}!='' and 4=#{status} and a.del_flag=1) or  \n" +
            "       (#{userIdAdmin} ='' and a.status=#{status} and a.del_flag=0)\n" +
            "    ) and \n" +
            "   user_id like CONCAT('%', #{userId}, '%') and  \n" +
            "   a.id like CONCAT('%', #{orderId}, '%') and\n" +
            "   a.book_id in (select id from book where book_name like CONCAT('%', #{bookName}, '%') and id=a.book_id)\n" +
            "order by\n" +
            "   a.del_flag asc, a.order_time desc ")
    List<OrderDto> getList(@Param("userId") String userId, @Param("orderId") String orderId,
                           @Param("bookName") String bookName,@Param("status") int status,@Param("userIdAdmin") String userIdAdmin);


    @Select("select \n" +
            "   id,order_time as orderTime,amount,price,status, \n" +
            "   (select name from address where id=a.address_id and del_flag=0) as name, \n" +
            "   (select phone from address where id=a.address_id and del_flag=0) as phone, \n" +
            "   (select detailed_address from address where id=a.address_id and del_flag=0)  as detailedAddress, \n" +
            "   (select book_name from book where id=a.book_id and del_flag=0) as bookName \n" +
            " from  \n" +
            "   order_form a\n" +
            " where \n" +
            "   a.id=#{orderId}")
    OrderDto getOne(String orderId);

    @Update("update order_form set status=#{status} where id=#{orderId}")
    void updateStatus(@Param("orderId") String orderId, @Param("status") int status);
}